package com.TestDemo.Test1;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2021/10/18/9:17
 * @Description:
 * @Target:测试Map的key存对象,并能取其属性,不能取属性..
 */
public class MapKeyIsObjectTest {
    public static void main(String[] args) {
        xPojo xp1 = new xPojo();
        xp1.setId(1);
        xp1.setName("zjz1");
        xPojo xp2 = new xPojo();
        xp2.setId(2);
        xp2.setName("zjz2");

        HashMap<xPojo, Integer> map = new HashMap<>();
        map.put(xp1,1);
        map.put(xp2,2);



        System.out.println(map.get(xp1.name));  // null 不通过
        System.out.println(map.get(xp2.name));  // null 不通过
        System.out.println(map.get(xp1));
        System.out.println(map.get(xp2));




    }




}


// 一个pojo对象
class xPojo{

    public int id;
    public String name;

    public xPojo() {
    }


    public xPojo(int id, String name) {
        this.id = id;
        this.name = name;
    }


    @Override
    public String toString() {
        return "xPojo{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
